from xml.sax import make_parser
from xml.sax.handler import ContentHandler

class SaxHandler(ContentHandler):
        """ Callback class for SAX parser """
	tasks = []
	curtask = {}
	segments = {}
	property = None
        chars = ""
	cursegment = None
	seg = None
	segf = None
	segt = None
	def __init__(self):
		property = None
		self.tasks = []
		self.chars = ""
		self.curtask = {}

        def startElement(self,name,attrs):
                """ Handle for element begin """
		if name == 'Task':
			self.curtask = {}
			self.curtask['sources'] = []
			self.curtask['segments'] = []
		if name == 'p':
			self.property = attrs.get('name')
		if name == "Segments":
			pass
		if name == 'segment':
			self.segf = attrs.get('from')
			self.segt = attrs.get('to')
			

			

        def endElement(self,name):
                """ Handle for finished element """
		if name == 'p':
			if self.property != 'source':
				self.curtask[self.property] = self.chars
			else:
				 self.curtask['sources'].append(self.chars)
		if name == 'Task':
			self.tasks.append(self.curtask)
		if name == 'segment':
			self.curtask['segments'].append((self.chars, self.segf, self.segt))
		if name == "Segments":
			pass	

        def characters (self,ch):
                """ Handle for character data """
                sch = ch.lstrip()
                if not sch == "":
                        self.chars = sch

